package com.cloudsigns.factorymanager2.produce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.BaseActivity;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.customview.ListViewForScrollView;
import com.cloudsigns.factorymanager2.produce.adapter.ProduceAddWorkpartAdapter1;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.iview.IGetProductWorkPartListView;
import com.cloudsigns.factorymanager2.produce.presenter.GetProductWorkPartListPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

public class ProduceAddWorkpartActivity1 extends BaseActivity implements IGetProductWorkPartListView {
    private RelativeLayout rl_back;
    private TextView tv_title;
    private ListViewForScrollView lv;
    private Button btn_add;

    private ProduceAddWorkpartAdapter1 adapter;
    private List<WorkPartBean> workPartList = new ArrayList<>();
    private GetProductWorkPartListPresenter getProductWorkPartListPresenter;
    private int selectedPositon;
    private WorkPartBean lastWorkPartBean;
    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.layout_produce_add_workpart1);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        lv = findViewById(R.id.lv);
        btn_add = findViewById(R.id.btn_add);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tv_title.setText(getResources().getString(R.string.add_process));
        getProductWorkPartListPresenter = new GetProductWorkPartListPresenter(this);
        getProductWorkPartListPresenter.getProductWorkPartList();
        lastWorkPartBean= (WorkPartBean) getIntent().getSerializableExtra(ConstantUtil.KEY_1);

        adapter = new ProduceAddWorkpartAdapter1(this, TAG);
        adapter.setData(workPartList);
        lv.setAdapter(adapter);
    }

    @Override
    public void setListener() {
        rl_back.setOnClickListener(this);
        btn_add.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 添加
            case R.id.btn_add:
                List<WorkPartBean> list = workPartList;
                ArrayList<WorkPartBean> workpartSelectedList = new ArrayList< >();
                for (WorkPartBean wpBean : list) {
                    if (wpBean.isChecked) {
                        workpartSelectedList.add(wpBean);

                        if(wpBean.isLastWorkPart()){

                        }
                    }
                }
                Intent mIntent = new Intent();
                mIntent.putExtra(ConstantUtil.KEY_2, workpartSelectedList);
                setResult(ConstantUtil.START_ACTIVITY_KEY_1, mIntent);
                finish();
                break;
        }
    }

    @Override
    public void getDataSuccessed(List<WorkPartBean> workPartLs ) {
        if (null != workPartLs && !workPartLs.isEmpty()) {
            workPartList.clear();
            workPartList.addAll(workPartLs);
            if(lastWorkPartBean!=null){
                for (WorkPartBean workPartBean:workPartList){
                    if(!TextUtils.isEmpty( workPartBean.getWorkPartName())&&workPartBean.getWorkPartName().equals(lastWorkPartBean.getWorkPartName())){
                        workPartBean.setLastWorkPart(true);
                        break;
                    }
                }
            }

            adapter.setData(workPartList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(desc);
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
